# Table of images
#
# Copyright (C) 2010 Yotam Medini <yotam.medini@gmail.com>
#
# This file is part of Gupicasa.
#
# Gupicasa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gupicasa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gupicasa.  If not, see <http://www.gnu.org/licenses/>.


import gtk

# local
import arith
import debug
import ugtk


class Mosaic(gtk.Table):


    def __init__(self, n_columns, label_n_selected):
        gtk.Table.__init__(self, 1, 1)
        self.selected_indices = set()
        self.current_button = None
        self.cbs = []
        self.n_columns = n_columns # 0 for square-like
        self.label_n_selected = label_n_selected
        self.show_n_selected()


    def place_images(self, n, pick):
        """
Place n picakble & selectable images in table.
  self:        The tk.Table widget
  n  :        Number of images
  pick:       pick callback with index
Return array of gtk.Image-s"""

        debug.log("")
        cs = self.get_children()
        debug.log("%d old children" % len(cs))
        for c in cs:
            self.remove(c)
        self.selected_indices = set()
        self.current_button = None
        self.show_n_selected()
        nrc = self.n_columns
        if nrc <= 0:
            nrc = arith.isqrt(n + 1) # +1 so  n=0  would give 1.
        debug.log("nrc=%d, n=%d, #tbl=%d" % (nrc, n, len(self.get_children())))
        map(lambda c: self.remove(c), self.get_children())
        self.resize(nrc, nrc)
        column = row = 0
        gis = n * [None]
        self.cbs = n * [None]
        group = None
        for i in range(n):

            hb = gtk.HBox()
            self.cbs[i] = cb = gtk.CheckButton()
            cb.connect('clicked', self.select, i)
            button = gtk.ToggleButton()
            button.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.Color("#0f0"))
            button.connect("clicked", self.set_current, pick, i)
            if group is None:
                group = button

            gi = gtk.Image()
            gis[i] = gi
            button.set_image(gi)
            button.show()
            hb.pack_start(cb, False, False, 0)
            hb.pack_start(button, True, True, 1)
            ugtk.attach1(self, hb, column, row, xpadding=4, ypadding=2)
            column += 1
            if i == 0:
                button.set_active(True)
            if column == nrc:
                column = 0
                row += 1
        self.show_all()
        return gis


    def select(self, w, i):
        debug.log("w=%s, i=%d" % (w, i))
        if w.get_active():
            self.selected_indices.add(i)
        else:
            self.selected_indices.remove(i)
        debug.log("si=%s" % self.selected_indices)
        self.show_n_selected()


    def set_current(self, w, pick, i):
        debug.log("w=%s, i=%d" % (w, i))
        if self.current_button is not None and self.current_button != w:
            self.current_button.set_active(False)
        self.current_button = w
        pick(w, i)


    def show_n_selected(self):
        self.label_n_selected.set_text("%d selected" % 
                                       len(self.selected_indices))
